package com.handinfo.uimenu.window;

import java.io.DataInputStream;
import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameManager;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIListener;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UIPopmenu;
import com.handinfo.uimenu.UITools;
import com.handinfo.uimenu.UIWindowFactory;
import com.handinfo.util.Tools;

public class UIFbactivity extends UIAbstractWindow
{
	private final int m_rectW = 4;
    private final int m_wordH = 2;
	private final int m_w_content = UITools.W_RECT_DRAG + m_rectW;//内容框的宽
	private final int m_h_content = AbstractGameScreen.FONT_H * 3 + 4 * m_wordH;//内容框高
	private final int m_w_label = AbstractGameScreen.FONT_W * 4 + 4*m_wordH;
	private final int m_h_label = AbstractGameScreen.FONT_H + 4*m_wordH;
	private final int m_NumColour = 0xFF0000;//数字颜色
	private final int m_WordColour = 0XFFFFFF;//特殊字颜色
	private final int m_Colour = 0xFFFF00;//标准字颜色
	private int m_h_drag;//滚动轴高
	private int m_plan = 0;//滚动百分比
	private int m_index =  0;//当前选择
	private int m_line_index;//当前行索引
	private int m_page_index;//当前页索引
	private int m_page_Line;//一页最大行
	private int m_page_num;//一共有多少页
	private byte m_num = 1;//绘制内容框数量
	private byte[] m_type;//内容类型 1及时 2次数
	private byte[] m_data1;//根据类型 表示不同 （1及时：0代表未开始 1代表立即参与）（2次数：当前次数）
	private byte[] m_data2;//根据类型表示不同 （1及时：无数据）（2次数：最大次数）
	private byte[] m_lv;//级别
	private String[] m_nameStrings;//名称
	private String[] m_timeStrings;//时间
	private String[] m_dscripStrings;//描述
	private String[] m_label;//标签
	private String[][] m_infos;//绘制内容
	
	public UIFbactivity(int id)
	{
		super(id);
		setBounds(UITools.X_BG, UITools.Y_BG, UITools.W_BG, UITools.H_BG);
	}

	public void init(){
		if( m_num <= 0 ){
			m_infos = null;
			return;
		}
		m_infos = new String[m_num][3];
		m_label = new String[m_num];
		StringBuffer buff = new StringBuffer();
		for(int i=0;i<m_num;i++){
			if( m_type[i] == 1 ){//及时
				if ( m_data1[i] == 0 ){
					m_label[i] = Tools.CHANGE_ROW+"未开始"+Tools.CHANGE_ROW;
				}else if( m_data1[i] == 1 ){
					m_label[i] = Tools.CHANGE_ROW+"立即参与"+Tools.CHANGE_ROW;
				}
			}else if( m_type[i] == 2 ){//次数
				buff.append(Tools.CHANGE_ROW);
				buff.append(m_data1[i]+"/"+m_data2[i]+"次");
				buff.append(Tools.CHANGE_ROW);
				m_label[i] = buff.toString();
				buff.delete(0, buff.toString().length());				
			}
			//拼名称
			buff.append(m_nameStrings[i]+" (" + m_lv[i] + ")级");
			m_infos[i][0] = buff.toString();
			buff.delete(0, buff.toString().length());
			//拼时间
			buff.append("时间： "+ Tools.CHANGE_ROW+ m_timeStrings[i]+Tools.CHANGE_ROW);
			m_infos[i][1] = buff.toString();
			buff.delete(0, buff.toString().length());
			//拼描述
			m_infos[i][2] = m_dscripStrings[i];
		   	
		}
		buff = null;
		m_line_index = 0;
	    m_page_index = 0;
		m_page_Line = UITools.H_RECT/m_h_content;
		boolean flag = false;
		m_page_num = 1;
		if( m_num > m_page_Line ){
			m_page_num = (m_num + m_page_Line-1)/m_page_Line;
			flag = true;
		}
		m_h_drag = flag?m_page_Line*m_h_content:m_num*m_h_content;
		checkIndex();
		checkDragPlan();
	}
	
	protected void paintContent(Graphics g)
	{
		//绘制背景
		UITools.paintBackGround(g, m_x, m_y, m_w, m_h, "活动", false, true);
		//绘制内容框
		int tempX = m_x + m_rectW;
		int tempY = m_y + UITools.INITY_CONTENT + m_wordH;
		int clipX = g.getClipX();
		int clipY = g.getClipY();
		int clipW = g.getClipWidth();
		int clipH = g.getClipHeight();
		g.setClip(tempX, tempY, m_w_content, m_h_drag);
		boolean flag = false;
		for (int i = m_page_index * m_page_Line; i < m_num; i++) {
			UITools.paintContentRect(g, tempX, tempY + (i - m_page_index * m_page_Line) * m_h_content, m_w_content, m_h_content);
			if (i == m_index) {//当前选中层
				UITools.fillContentRect(g, tempX, tempY + (i - m_page_index * m_page_Line) * m_h_content, m_w_content, m_h_content, UIManager
						.getPaletteColor(UIManager.COLOR_RECT_SEL));
				flag = true;
			}
			//绘制内容
			paintInfos(g, i,tempX, tempY + (i - m_page_index * m_page_Line) * m_h_content);
			//绘制标签
			int labelX = tempX + m_w_content - 2* m_wordH - m_w_label;
			int labelY = tempY + (( m_h_content - m_h_label)>>1) + (i - m_page_index * m_page_Line) * m_h_content;
			UITools.paintLabel(g, labelX, labelY, m_w_label, m_h_label, flag);
			specialString(g, labelX + 2*m_wordH, labelY + 2*m_wordH, m_label[i], m_NumColour, m_WordColour, m_Colour);
			flag = false;
		}
		//绘制滑动滚轴
		tempX = m_x + UITools.X_DRAG;
		UITools.paintScrollBar(g, tempX, tempY, m_h_drag, m_plan);
		//绘制翻页
		if (m_page_num != 0) {
			UITools.paintPage(g, m_x + (m_w >> 1), m_y + m_h - 12, m_page_index, m_page_num,false,false);
		}
		g.setClip(clipX, clipY, clipW, clipH);
	}
	
	//绘制内容
	protected void paintInfos(Graphics g, int num, int x, int y)
	{
		int tempX = x + m_rectW;
		int tempY = y + m_wordH;
		specialString(g, tempX, tempY, m_infos[num][0], m_NumColour, m_WordColour, m_Colour);
		specialString(g, tempX, tempY + m_wordH + AbstractGameScreen.FONT_H, m_infos[num][1], m_NumColour, m_WordColour, m_Colour);
		specialString(g, tempX, tempY + m_wordH * 2 + AbstractGameScreen.FONT_H*2, m_infos[num][2], m_NumColour, m_WordColour, m_Colour);
	}

	//绘制带颜色数字字符串
		protected void specialString(Graphics g,int x,int y,String Str,int NumColour,int WordColour,int Colour){
			if( Str != null ){	
				int size = Str.length();
				int width = 0;
				boolean flag = false;
				for(int i=0;i<size;i++){
					char temp = Str.charAt(i);
					if( temp == Tools.CHANGE_ROW ){
						flag = flag?false:true;
						g.setColor(WordColour);					
					}else{
						if( temp >=48 && temp <= 57 ){
							g.setColor(NumColour);
						}else {
							if(!flag){
								g.setColor(Colour);
							}else{
								g.setColor(WordColour);
							}
						}
						g.drawChar(temp, x+width, y, 0);
						width += AbstractGameScreen.s_font.charWidth(temp);
					}	
				}
			}
		} 
		
	protected void keyLogic()
	{
		if( AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_UP)){
			if( m_line_index > 0 ){
				m_line_index --;
			}else{
				if( m_page_index > 0 ){
					m_page_index --;
					m_line_index = m_page_Line -1;
				}
			}
			checkIndex();
			checkDragPlan();
		}else if( AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_DOWN)){
			if( m_line_index < m_page_Line - 1){
				if( m_index < m_num -1){
					m_line_index ++;
				}
			}else{
				if( m_page_index < m_page_num - 1){
					m_page_index ++;
					m_line_index = 0;
				}
			}
			checkIndex();
			checkDragPlan();
		}else if(AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
				|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)){
			boolean flag = false;
			String temp = "";
			switch(m_type[m_index]){
				case 1://及时
					if( m_data1[m_index] == 0 ){//未开始
						temp = "活动尚未开始！";
					}else if( m_data1[m_index] == 1 ) {//立即参与
						flag = true;
					}
					break;
				case 2://计数
					if( m_data1[m_index] < m_data2[m_index] ){//数量没够
						flag = true;
					}else{
						temp = "活动次数已达上限！";
					}
					break;
			}
			
			if(flag){
				final UIPopmenu pop1 = (UIPopmenu) UIManager.getInstance().createWindow(
						UIWindowFactory.WINDOW_POPMENU);
				pop1.setPopmenus(new String[] {
						"立即参与"});
				pop1.addListener(new UIListener(){

					public void actionPerformed()
					{
						if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
								|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
							GameManager.getInstance().m_game_message.sendMustJion();
							UIManager.getInstance().removeWindow(pop1);
						}else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
							UIManager.getInstance().removeWindow(pop1);
						}
						
					}	
				});
			}else{
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, temp);
			}			
		}else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
			close();
		}
		
	}
    
	protected void checkIndex(){
		m_index = m_page_index*m_page_Line + m_line_index;
	}
	
	protected void checkDragPlan(){
		int max = m_num <= 1?2:m_num;
	    m_plan = m_index*100/(max-1);
	}
	
	/**
	 * 接收采集
	 * */
	public static final void recvUIFbactivity(DataInputStream dis)
	{
		try {
			Tools.debugPrintln("recvUIFbactivity");
			byte state = dis.readByte();
            if( state == 1 ){
            	UIFbactivity win = (UIFbactivity) UIWindowFactory.createWindow(UIWindowFactory.WINDOW_ID_FBACTIVITY);
            	byte num = dis.readByte();
            	win.m_num = num;
            	win.m_type = new byte[num];
            	win.m_data1 = new byte[num];
            	win.m_data2 = new byte[num];
            	win.m_lv = new byte[num];
            	win.m_nameStrings = new String[num];
            	win.m_timeStrings = new String[num];
            	win.m_dscripStrings = new String[num];
            	for(int i=0;i<num;i++){	
            		Tools.debugPrintln("i="+i);
            		win.m_type[i] = dis.readByte();
            		Tools.debugPrintln("win.m_type="+win.m_type[i]);
            		win.m_data1[i] = dis.readByte();
            		Tools.debugPrintln("win.m_data1="+win.m_data1[i]);
            		win.m_data2[i] = dis.readByte();
            		Tools.debugPrintln("win.m_data2="+win.m_data2[i]);
            		win.m_lv[i] = dis.readByte();
            		Tools.debugPrintln("win.m_lv="+win.m_lv[i]);
            		win.m_nameStrings[i] = dis.readUTF();
            		Tools.debugPrintln("win.m_nameStrings="+win.m_nameStrings[i]);
            		win.m_timeStrings[i] = dis.readUTF();
            		Tools.debugPrintln("win.m_timeStrings="+win.m_timeStrings[i]);
            		win.m_dscripStrings[i] = dis.readUTF();
            		Tools.debugPrintln("win.m_dscripStrings="+win.m_dscripStrings[i]);
            	}
            	win.init();
            	UIManager.getInstance().addWindow(win);
            }        
			dis.close();
			dis = null;
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}
}
